package com.example.ll_video.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ll_video.bean.PayMoney;
import com.example.ll_video.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class PayMoneyAdapter extends RecyclerView.Adapter<PayMoneyAdapter.ViewHolder> {


    private Context context;
    private ItemClickListener itemClickListener;
    private List<PayMoney> myData;
    private int selectedPosition = 0;
    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public PayMoneyAdapter(Context context, List<PayMoney> myData) {
        this.context = context;
        this.myData = myData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.pay_money_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PayMoney payMoney = myData.get(position);
        if (payMoney.isSelected()) {
            holder.llPayMoney.setBackgroundResource(R.drawable.btn_blue_5_bg);
            holder.tvVip.setTextColor(context.getResources().getColor(R.color.white));
            holder.tvPrice.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            holder.llPayMoney.setBackgroundResource(R.drawable.btn_gray_line_5_bg);
            holder.tvVip.setTextColor(context.getResources().getColor(R.color.text_color));
            holder.tvPrice.setTextColor(context.getResources().getColor(R.color.text_color));
        }

        holder.tvPrice.setText(payMoney.getPrice());
        holder.tvVip.setText(payMoney.getMemo());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position, payMoney);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_vip)
        TextView tvVip;
        @BindView(R.id.ll_pay_money)
        LinearLayout llPayMoney;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface ItemClickListener {
        void onItemClick(int position, PayMoney payMoney);
    }

    public void refresh(int position) {
        if (position != selectedPosition){
           PayMoney payMoney1 = myData.get(selectedPosition);
           payMoney1.setSelected(false);
           notifyItemChanged(selectedPosition,payMoney1);

            PayMoney payMoney2 = myData.get(position);
            payMoney2.setSelected(true);
            notifyItemChanged(position,payMoney2);

            selectedPosition = position;
        }
    }
}
